package com.elitesland.system.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.exception.BusinessException;
import com.elitesland.system.param.SysPermissionNewParam;
import com.elitesland.system.param.SysPermissionUpdateParam;
import com.elitesland.system.service.SysPermissionService;
import com.elitesland.system.vo.SysPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <pre>
 * [功能说明]
 * </pre>
 *
 * @author Mir
 * @date 2020/7/5
 */
@RestController
@Slf4j
@RequestMapping("/sys/permissions")
@Api(value = "系统权限，菜单、操作管理", tags = {"系统权限，菜单、操作管理"})
@RequiredArgsConstructor
public class SysPermissionController {

    private final SysPermissionService sysPermissionService;

    @PostMapping("/newmenu")
    @ApiOperation("创建菜单")
    public ApiResult<Object> createMenu(@RequestBody @Validated SysPermissionNewParam permission) throws BusinessException {
        val id = sysPermissionService.createMenu(permission);
        return ApiResult.ok(id);
    }

    @PostMapping("/newaction")
    @ApiOperation("在菜单下创建操作，比如按钮")
    public ApiResult<Object> createAction(@RequestBody SysPermissionNewParam permission) throws BusinessException {
        val id = sysPermissionService.createAction(permission);
        return ApiResult.ok(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过ID获取菜单或能力明细信息")
    public ApiResult<SysPermissionVO> oneById(@PathVariable Long id) {
        return sysPermissionService.getById(id).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @GetMapping("/actionsbymenu/{id}")
    @ApiOperation("通过菜单ID，获取其下对应的能力清单")
    public ApiResult<List<SysPermissionVO>> listActionsByMenuId(@PathVariable Long id) {
        return ApiResult.ok(sysPermissionService.listActionsByMenuId(id));
    }

    @PutMapping
    @ApiOperation("更新权限信息")
    public ApiResult<Integer> update(@RequestBody SysPermissionUpdateParam perm) {
        sysPermissionService.update(perm);
        return ApiResult.ok(1);
    }

    @GetMapping("/menutree")
    @ApiOperation("获取菜单树")
    public ApiResult<Object> getMenuTree() {
        val tree = sysPermissionService.listAllMenuTree();
        return ApiResult.ok(tree);
    }

    @GetMapping("/menuactiontree")
    @ApiOperation("获取菜单能力树")
    public ApiResult<Object> getMenuActionTree() {
        val tree = sysPermissionService.listAllMenuWithActionTree();
        return ApiResult.ok(tree);
    }

    @GetMapping("/menuacttree/{permId}")
    @ApiOperation("获取菜单和操作树")
    public ApiResult<Object> getMenuActionTree(@PathVariable Long permId) {
        val tree = sysPermissionService.listAllMenuWithActionTree(permId);
        return ApiResult.ok(tree);
    }
}
